<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>决策树实现Titanic(重要) | Miki&#39;s blog</title>
  <meta name="keywords" content=" 机器学习 ">
  <meta name="description" content="决策树实现Titanic(重要) | Miki&#39;s blog">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description" content="keywords: author: Miki language: zh-CN timezone:">
<meta property="og:type" content="website">
<meta property="og:title" content="标签">
<meta property="og:url" content="http://mikiblog.online/tags/index.html">
<meta property="og:site_name" content="Miki&#39;s blog">
<meta property="og:description" content="keywords: author: Miki language: zh-CN timezone:">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-02-22T01:48:02.000Z">
<meta property="article:modified_time" content="2020-02-22T01:50:40.651Z">
<meta property="article:author" content="Miki Zhu">
<meta name="twitter:card" content="summary">


<link rel="icon" href="/img/1.jpg">

<link href="/css/style.css?v=1.0.1" rel="stylesheet">

<link href="/css/hl_theme/atom-dark.css?v=1.0.1" rel="stylesheet">

<link href="//cdn.bootcss.com/animate.css/3.5.2/animate.min.css" rel="stylesheet">
<link href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="/js/jquery.autocomplete.min.js?v=1.0.1" ></script>

<script src="//cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script>
<script>
    hljs.initHighlightingOnLoad();
</script>

<script src="//cdn.bootcss.com/nprogress/0.2.0/nprogress.min.js"></script>



<script src="/js/iconfont.js?v=1.0.1" ></script>

<meta name="generator" content="Hexo 4.2.0"></head>
<div style="display: none">
  <input class="theme_disqus_on" value="false">
  <input class="theme_preload_comment" value="false">
  <input class="theme_blog_path" value="">
</div>

<body>
<aside class="nav">
    <div class="nav-left">
        <a href="/" class="avatar_target">
    <img class="avatar" src="/img/头像.jpg" />
</a>
<div class="author">
    <span>Miki Zhu</span>
</div>

<div class="icon">
    
        
    
        
        <a title="email" href="mailto:747876457@qq.com" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-email"></use>
                </svg>
            
        </a>
        
    
        
        <a title="qq" href="http://wpa.qq.com/msgrd?v=3&uin=747876457&site=qq&menu=yes" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-qq"></use>
                </svg>
            
        </a>
        
    
</div>




<ul>
    <li><div class="all active">全部文章<small>(29)</small></div></li>
    
        
            
            <li><div data-rel="机器学习">机器学习<small>(7)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="机器学习实战">机器学习实战<small>(3)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="项目实战">项目实战<small>(4)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="博客搭建">博客搭建<small>(2)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="算法">算法<small>(3)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="python教程">python教程<small>(1)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="学习网站推荐">学习网站推荐<small>(1)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="考研经验">考研经验<small>(1)</small></div>
                
            </li>
            
        
    
</ul>
<div class="left-bottom">
    <div class="menus">
    
    
    
    
    </div>
    <div><a class="about  " target="_blank" href="/about">关于</a></div>
</div>
<input type="hidden" id="yelog_site_posts_number" value="29">
<input type="hidden" id="yelog_site_word_count" value="62.1k">
<div style="display: none">
    <span id="busuanzi_value_site_uv"></span>
    <span id="busuanzi_value_site_pv"></span>
</div>

    </div>
    <div class="nav-right">
        <div class="friends-area">
    <div class="friends-title">
        友情链接
        <i class="back-title-list"></i>
    </div>
    <div class="friends-content">
        <ul>
            
        </ul>
    </div>
</div>
        <div class="title-list">
    <form onkeydown="if(event.keyCode === 13){return false;}">
        <input id="local-search-input" class="search" type="text" placeholder="Search..." />
        <i class="cross"></i>
        <span>
            <label for="tagswitch">Tags:</label>
            <input id="tagswitch" type="checkbox" style="display: none" />
            <i id="tagsWitchIcon"></i>
        </span>
    </form>
    <div class="tags-list">
    
    <li class="article-tag-list-item">
        <a class="color5">机器学习</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">算法</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">博客搭建</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color2">python</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">网站推荐</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">考研经验</a>
    </li>
    
    <div class="clearfix"></div>
</div>

    
    <nav id="title-list-nav">
        
        <a id="top" class="python教程 "
           href="/2020/04/03/python%E9%9D%A2%E8%AF%95%E9%A2%98/"
           data-tag="python"
           data-author="" >
            <span class="post-title" title="python面试题(不定期更新)">python面试题(不定期更新)</span>
            <span class="post-date" title="2020-04-03 21:14:43">2020/04/03</span>
        </a>
        
        <a id="top" class="考研经验 "
           href="/2020/02/20/%E8%80%83%E7%A0%94%E5%88%9D%E8%AF%95%E7%BB%8F%E9%AA%8C/"
           data-tag="考研经验"
           data-author="" >
            <span class="post-title" title="考研初试经验">考研初试经验</span>
            <span class="post-date" title="2020-02-20 19:33:48">2020/02/20</span>
        </a>
        
        <a  class="机器学习实战 "
           href="/2020/04/19/%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="逻辑回归代码实现">逻辑回归代码实现</span>
            <span class="post-date" title="2020-04-19 21:09:25">2020/04/19</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/04/14/%E9%A9%AC%E5%B0%94%E5%8F%AF%E5%A4%AB%E9%93%BE/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="马尔可夫链">马尔可夫链</span>
            <span class="post-date" title="2020-04-14 19:09:29">2020/04/14</span>
        </a>
        
        <a  class="机器学习实战 "
           href="/2020/04/14/%E5%86%B3%E7%AD%96%E6%A0%91%E5%AE%9E%E6%88%98%E9%9A%90%E5%BD%A2%E7%9C%BC%E9%95%9C%E5%88%86%E7%B1%BB/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="决策树实战隐形眼镜分类">决策树实战隐形眼镜分类</span>
            <span class="post-date" title="2020-04-14 10:49:40">2020/04/14</span>
        </a>
        
        <a  class="机器学习实战 "
           href="/2020/04/12/KNN%E5%AE%9E%E6%88%98%E4%B9%8B%E6%B5%B7%E4%BC%A6%E7%BA%A6%E4%BC%9A/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="KNN实战之海伦约会">KNN实战之海伦约会</span>
            <span class="post-date" title="2020-04-12 08:22:12">2020/04/12</span>
        </a>
        
        <a  class="博客搭建 "
           href="/2020/04/08/hexo-%E4%B8%BB%E9%A2%98%E6%B7%BB%E5%8A%A0%E8%AF%84%E8%AE%BA/"
           data-tag="博客搭建"
           data-author="" >
            <span class="post-title" title="hexo 主题添加评论">hexo 主题添加评论</span>
            <span class="post-date" title="2020-04-08 21:43:26">2020/04/08</span>
        </a>
        
        <a  class="项目实战 "
           href="/2020/03/30/%E6%98%8E%E5%A4%A9%E4%BC%9A%E4%B8%8B%E9%9B%A8%E5%90%97%EF%BC%9F/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="明天会下雨吗？">明天会下雨吗？</span>
            <span class="post-date" title="2020-03-30 11:25:31">2020/03/30</span>
        </a>
        
        <a  class="项目实战 "
           href="/2020/03/23/%E5%86%B3%E7%AD%96%E6%A0%91%E5%AE%9E%E7%8E%B0Titanic-%E9%87%8D%E8%A6%81/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="决策树实现Titanic(重要)">决策树实现Titanic(重要)</span>
            <span class="post-date" title="2020-03-23 17:09:05">2020/03/23</span>
        </a>
        
        <a  class="项目实战 "
           href="/2020/03/21/Titanic-%E9%A1%B9%E7%9B%AE%E8%AF%A6%E8%A7%A3/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="Titanic 项目详解">Titanic 项目详解</span>
            <span class="post-date" title="2020-03-21 14:07:20">2020/03/21</span>
        </a>
        
        <a  class="项目实战 "
           href="/2020/03/19/kaggle%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="kaggle项目实战，Titanic analysis">kaggle项目实战，Titanic analysis</span>
            <span class="post-date" title="2020-03-19 10:08:13">2020/03/19</span>
        </a>
        
        <a  class=""
           href="/2020/03/17/Pytorch-%E6%90%AD%E5%BB%BA%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Pytorch 搭建神经网络">Pytorch 搭建神经网络</span>
            <span class="post-date" title="2020-03-17 14:56:16">2020/03/17</span>
        </a>
        
        <a  class=""
           href="/2020/03/16/CNN%E5%8E%9F%E7%90%86/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="CNN">CNN</span>
            <span class="post-date" title="2020-03-16 09:11:00">2020/03/16</span>
        </a>
        
        <a  class=""
           href="/2020/03/14/%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97%E5%92%8Ck%E5%9D%87%E5%80%BC/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="random forest and k means">random forest and k means</span>
            <span class="post-date" title="2020-03-14 16:29:22">2020/03/14</span>
        </a>
        
        <a  class=""
           href="/2020/03/13/%E6%94%AF%E6%8C%81%E5%90%91%E9%87%8F%E6%9C%BA%E7%AE%97%E6%B3%95/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="支持向量机">支持向量机</span>
            <span class="post-date" title="2020-03-13 20:53:20">2020/03/13</span>
        </a>
        
        <a  class=""
           href="/2020/03/11/Matplotlab/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="Matplotlab">Matplotlab</span>
            <span class="post-date" title="2020-03-11 10:13:20">2020/03/11</span>
        </a>
        
        <a  class=""
           href="/2020/03/10/pandas/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="pandas">pandas</span>
            <span class="post-date" title="2020-03-10 09:20:27">2020/03/10</span>
        </a>
        
        <a  class=""
           href="/2020/03/09/logical-regression/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="logical regression">logical regression</span>
            <span class="post-date" title="2020-03-09 20:30:33">2020/03/09</span>
        </a>
        
        <a  class="学习网站推荐 "
           href="/2020/03/08/%E5%AD%A6%E4%B9%A0%E7%BD%91%E7%AB%99%E6%88%96%E5%B7%A5%E5%85%B7%E6%8E%A8%E8%8D%90/"
           data-tag="网站推荐"
           data-author="" >
            <span class="post-title" title="学习网站或工具推荐">学习网站或工具推荐</span>
            <span class="post-date" title="2020-03-08 20:44:55">2020/03/08</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/03/08/%E8%B4%9D%E5%8F%B6%E6%96%AF%E5%88%86%E6%9E%90/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="贝叶斯分析">贝叶斯分析</span>
            <span class="post-date" title="2020-03-08 20:11:15">2020/03/08</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/03/08/numpy/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="numpy的使用">numpy的使用</span>
            <span class="post-date" title="2020-03-08 19:15:54">2020/03/08</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/03/07/%E5%86%B3%E7%AD%96%E6%A0%91/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="决策树">决策树</span>
            <span class="post-date" title="2020-03-07 19:27:38">2020/03/07</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/03/06/K-%E8%BF%91%E9%82%BB%E7%AE%97%E6%B3%95/"
           data-tag="算法"
           data-author="" >
            <span class="post-title" title="K-近邻算法">K-近邻算法</span>
            <span class="post-date" title="2020-03-06 20:18:19">2020/03/06</span>
        </a>
        
        <a  class="算法 "
           href="/2020/03/05/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/"
           data-tag="算法"
           data-author="" >
            <span class="post-title" title="排序算法">排序算法</span>
            <span class="post-date" title="2020-03-05 19:41:32">2020/03/05</span>
        </a>
        
        <a  class="算法 "
           href="/2020/03/04/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E6%97%B6%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A6/"
           data-tag="算法"
           data-author="" >
            <span class="post-title" title="数据结构与时间复杂度">数据结构与时间复杂度</span>
            <span class="post-date" title="2020-03-04 21:00:32">2020/03/04</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/03/04/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E6%95%B0%E5%AD%A6%E5%9F%BA%E7%A1%80/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="机器学习数学基础">机器学习数学基础</span>
            <span class="post-date" title="2020-03-04 16:48:34">2020/03/04</span>
        </a>
        
        <a  class="算法 "
           href="/2020/03/03/leetcode%E7%AE%97%E6%B3%95%E7%BB%83%E4%B9%A0/"
           data-tag="算法"
           data-author="" >
            <span class="post-title" title="哈希图 and 移动窗口">哈希图 and 移动窗口</span>
            <span class="post-date" title="2020-03-03 18:49:52">2020/03/03</span>
        </a>
        
        <a  class="机器学习 "
           href="/2020/03/01/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E5%9F%BA%E6%9C%AC%E6%9C%AF%E8%AF%AD/"
           data-tag="机器学习"
           data-author="" >
            <span class="post-title" title="1 零基础学机器学习">1 零基础学机器学习</span>
            <span class="post-date" title="2020-03-01 20:23:40">2020/03/01</span>
        </a>
        
        <a  class="博客搭建 "
           href="/2020/02/20/Windows%E7%B3%BB%E7%BB%9F%E4%B8%8B%E7%94%A8hexo%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2/"
           data-tag="博客搭建"
           data-author="" >
            <span class="post-title" title="Windows系统下用hexo搭建个人博客">Windows系统下用hexo搭建个人博客</span>
            <span class="post-date" title="2020-02-20 13:15:25">2020/02/20</span>
        </a>
        
    </nav>
</div>
    </div>
    <div class="hide-list">
        <div class="semicircle">
            <div class="brackets first"><</div>
            <div class="brackets">&gt;</div>
        </div>
    </div>
</aside>
<div class="post">
    <div class="pjax">
        <article id="post-决策树实现Titanic-重要" class="article article-type-post" itemscope itemprop="blogPost">
    
        <h1 class="article-title">决策树实现Titanic(重要)</h1>
    
    <div class="article-meta">
        
        
        
        <span class="book">
            
                <a  data-rel="项目实战">项目实战</a>
            
        </span>
        
        
        <span class="tag">
            
            <a class="color5">机器学习</a>
            
        </span>
        
    </div>
    <div class="article-meta">
        
        创建时间:<time class="date" title='更新时间: 2020-03-23 17:13:59'>2020-03-23 17:09</time>
        
    </div>
    <div class="article-meta">
        
        <span>字数:2.2k</span>
        
        
        <span id="busuanzi_container_page_pv">
            阅读:<span id="busuanzi_value_page_pv">
                <span class="count-comment">
                    <span class="spinner">
                      <div class="cube1"></div>
                      <div class="cube2"></div>
                    </span>
                </span>
            </span>
        </span>
        
        
        <span class="top-comment" title="跳转至评论区">
            <a href="#comments">
                评论:<span class="count-comment">
                    <span class="spinner">
                      <div class="cube1"></div>
                      <div class="cube2"></div>
                    </span>
                </span>
            </a>
        </span>
        
    </div>
    
    <div class="toc-ref">
    
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#推荐书籍"><span class="toc-text"> 推荐书籍</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#决策树"><span class="toc-text"> 决策树</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#决策树如何找到关键特征"><span class="toc-text"> 决策树如何找到关键特征</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#过拟合和欠拟合"><span class="toc-text"> 过拟合和欠拟合</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#泛化能力定义"><span class="toc-text"> 泛化能力定义</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#sklearn中实现决策树代码"><span class="toc-text"> sklearn中实现决策树(代码)</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#titanic项目实战"><span class="toc-text"> Titanic项目实战</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#数据的处理"><span class="toc-text"> 数据的处理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#读取数据"><span class="toc-text"> 读取数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#显示数据"><span class="toc-text"> 显示数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#查看数据缺失值"><span class="toc-text"> 查看数据缺失值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#对缺失值的处理"><span class="toc-text"> 对缺失值的处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#查看特征里变量的个数"><span class="toc-text"> 查看特征里变量的个数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#统计特征中变量出现的次数"><span class="toc-text"> 统计特征中变量出现的次数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#数据类型转换"><span class="toc-text"> 数据类型转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#将标签和特征进行分离"><span class="toc-text"> 将标签和特征进行分离</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#算法实现"><span class="toc-text"> 算法实现</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#数据再处理"><span class="toc-text"> 数据再处理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#训练算法加预测"><span class="toc-text"> 训练算法加预测</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#模型改进"><span class="toc-text"> 模型改进</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#交叉验证"><span class="toc-text"> 交叉验证</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#参数调节-重要"><span class="toc-text"> 参数调节 (重要)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#观察拟合程度"><span class="toc-text"> 观察拟合程度</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#网格搜索"><span class="toc-text"> 网格搜索</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束"><span class="toc-text"> 结束</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#分析结果保存"><span class="toc-text"> 分析结果保存</span></a></li></ol></li></ol></li></ol>
    
<style>
    .left-col .switch-btn,
    .left-col .switch-area {
        display: none;
    }
    .toc-level-3 i,
    .toc-level-3 ol {
        display: none !important;
    }
</style>
</div>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>[TOC]</p>
<h1 id="推荐书籍"><a class="markdownIt-Anchor" href="#推荐书籍"></a> 推荐书籍</h1>
<ol>
<li>数据挖掘导论 只涉及到理论</li>
<li>机器学习(周志华)涉及到挺多的数学原理</li>
</ol>
<h1 id="决策树"><a class="markdownIt-Anchor" href="#决策树"></a> 决策树</h1>
<ul>
<li>决策树对数据的要求不高，可以是数值型，或者字符串类型</li>
</ul>
<h2 id="决策树如何找到关键特征"><a class="markdownIt-Anchor" href="#决策树如何找到关键特征"></a> 决策树如何找到关键特征</h2>
<ol>
<li>如何从数据的特征中找到最佳节点和最佳分支</li>
<li>如何停止决策树增长，防止过拟合</li>
</ol>
<h2 id="过拟合和欠拟合"><a class="markdownIt-Anchor" href="#过拟合和欠拟合"></a> 过拟合和欠拟合</h2>
<ol>
<li>过拟合</li>
</ol>
<p>在训练集上表现很好，但是在测试集上表现很遭<br />
2. 欠拟合</p>
<p>在训练中学习的就不是很好，在测试的时候依旧表现很差，这就是欠拟合</p>
<h2 id="泛化能力定义"><a class="markdownIt-Anchor" href="#泛化能力定义"></a> 泛化能力定义</h2>
<p>有时候，模型能够对训练集的数据学习的很好，当我们往训练集加入一些新的具有同样特征的数据时，模型得到的结果不尽人意，则该模型的泛化能力比较低</p>
<h2 id="sklearn中实现决策树代码"><a class="markdownIt-Anchor" href="#sklearn中实现决策树代码"></a> sklearn中实现决策树(代码)</h2>
<p><strong>决策树实现代码：</strong></p>
<ol>
<li>从sklearn中导入相关函数</li>
</ol>
<pre class="highlight"><code class="">from sklearn import tree
</code></pre>
<ol start="2">
<li>对类进行实例化</li>
</ol>
<pre class="highlight"><code class="">clf = tree.DecisionTreeclassifier()
</code></pre>
<ol start="3">
<li>用训练集训练模型</li>
</ol>
<pre class="highlight"><code class="">clf = clf.fit(x_train, y_train)
</code></pre>
<ol start="4">
<li>返回预测的准确度</li>
</ol>
<pre class="highlight"><code class="">result = clf.score(x_test, y_test)
</code></pre>
<blockquote>
<p><strong>上面的几个函数，几乎适用于所有的sklearn算法</strong></p>
</blockquote>
<h1 id="titanic项目实战"><a class="markdownIt-Anchor" href="#titanic项目实战"></a> Titanic项目实战</h1>
<h2 id="数据的处理"><a class="markdownIt-Anchor" href="#数据的处理"></a> 数据的处理</h2>
<blockquote>
<p>比赛中，我们经常要对训练集和测试集进行同样的操作，数据预处理，所以我们可以将训练集和测试集放到列表中，用for循环来操作</p>
</blockquote>
<h3 id="读取数据"><a class="markdownIt-Anchor" href="#读取数据"></a> 读取数据</h3>
<pre class="highlight"><code class="">train = pd.read_csv(r'C:\Users\asus\Desktop\train.csv')
</code></pre>
<h3 id="显示数据"><a class="markdownIt-Anchor" href="#显示数据"></a> 显示数据</h3>
<pre class="highlight"><code class=""># 显示原始数据
train.head(5)
# 显示数据的平均值，最大最小值等信息
train.describe()
</code></pre>
<h3 id="查看数据缺失值"><a class="markdownIt-Anchor" href="#查看数据缺失值"></a> 查看数据缺失值</h3>
<pre class="highlight"><code class="">train.info()
</code></pre>
<p>观察下面数据：</p>
<pre class="highlight"><code class="">PassengerId    891 non-null int64
Survived       891 non-null int64
Pclass         891 non-null int64
Name           891 non-null object
Sex            891 non-null object
Age            714 non-null float64
SibSp          891 non-null int64
Parch          891 non-null int64
Ticket         891 non-null object
Fare           891 non-null float64
Cabin          204 non-null object
Embarked       889 non-null object
</code></pre>
<blockquote>
<p>我们可以发现三个属性都有缺失值</p>
</blockquote>
<h3 id="对缺失值的处理"><a class="markdownIt-Anchor" href="#对缺失值的处理"></a> 对缺失值的处理</h3>
<ol>
<li>对年龄这一栏数据，我们可以用年龄的平均值，对缺失值进行填充</li>
</ol>
<p>获得年龄的平均值</p>
<pre class="highlight"><code class="">train.Age.mean()
</code></pre>
<p>对缺失值填充</p>
<pre class="highlight"><code class="">train.Age = train.Age.fillna(train.Age.mean())
</code></pre>
<ol start="2">
<li>船舱这一栏数据缺失值太多，我们将这一列删除</li>
</ol>
<pre class="highlight"><code class="">train.drop(['Cabin'],axis=1, inplace=True)
</code></pre>
<ul>
<li>属性axis=1表示对列操作，默认是对行操作</li>
<li>inplace表示是否替代原来的数据，默认是不替换</li>
<li>删除只能用列表里面填入要删除的标签</li>
</ul>
<p>这里我们对数据进行简化，删除下面特征</p>
<pre class="highlight"><code class="">train.drop(['Name','Ticket','PassengerId'], inplace=True, axis=1)
</code></pre>
<ol start="3">
<li>登船入口缺失了两个，统计登船入口个数，用概率最大的进行填充</li>
</ol>
<p>模块的使用下面会讲到</p>
<ul>
<li>可以发现S出现次数最多，因此用S对数据进行填充</li>
</ul>
<pre class="highlight"><code class="">Counter({'S': 644, 'C': 168, 'Q': 77, nan: 2})
train['Embarked'] = train['Embarked'].fillna('S')
</code></pre>
<h3 id="查看特征里变量的个数"><a class="markdownIt-Anchor" href="#查看特征里变量的个数"></a> 查看特征里变量的个数</h3>
<pre class="highlight"><code class="">train['Sex'].unique()
train['Embarked'].unique()
</code></pre>
<p>输出：</p>
<pre class="highlight"><code class="">array(['male', 'female']
array(['S', 'C', 'Q', nan]
</code></pre>
<h3 id="统计特征中变量出现的次数"><a class="markdownIt-Anchor" href="#统计特征中变量出现的次数"></a> 统计特征中变量出现的次数</h3>
<ul>
<li>这里我们要引入一个新的模块</li>
</ul>
<pre class="highlight"><code class="">from collections import Counter
Counter(train['Embarked'])
</code></pre>
<p>输出：</p>
<pre class="highlight"><code class="">array(['S', 'C', 'Q'], dtype=object)
</code></pre>
<h3 id="数据类型转换"><a class="markdownIt-Anchor" href="#数据类型转换"></a> 数据类型转换</h3>
<ul>
<li>利用map()将输出进行转换</li>
<li>注意变换不会改变原始数据</li>
</ul>
<pre class="highlight"><code class="">map1 = {'male':0
    ,'female':1
        }
map2 = {'S':0
    ,'C':1
    ,'Q':2
        }
train['Sex'] = train['Sex'].map(map1)
train['Embarked'] = train['Embarked'].map(map2)
</code></pre>
<h3 id="将标签和特征进行分离"><a class="markdownIt-Anchor" href="#将标签和特征进行分离"></a> 将标签和特征进行分离</h3>
<ul>
<li>在所给的训练数据中，我们可以发现，标签和特征是在一起的，也就是<code>x_train</code>,<code>y_train</code></li>
</ul>
<p><strong>现在将两者进行分离</strong></p>
<p><code>iloc</code>可以按数字索引(里面有个i，int)</p>
<p><code>loc</code>可以按标签索引</p>
<ul>
<li>可以发现布尔运算输出是布尔值</li>
</ul>
<pre class="highlight"><code class="">train.columns == 'Survived'
</code></pre>
<pre class="highlight"><code class="">array([ True, False, False, False, False, False, False, False])
</code></pre>
<p><strong>对数据进行分离</strong></p>
<pre class="highlight"><code class="">x_train = train.iloc[:,train.columns != 'Survived']
y_train = train.iloc[:,train.columns == &quot;Survived&quot;]
# 将列标签转换成列表
x_train.columns.tolist()
</code></pre>
<p><strong>可以发现将标签分离了</strong></p>
<pre class="highlight"><code class="">['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
</code></pre>
<h2 id="算法实现"><a class="markdownIt-Anchor" href="#算法实现"></a> 算法实现</h2>
<h3 id="数据再处理"><a class="markdownIt-Anchor" href="#数据再处理"></a> 数据再处理</h3>
<p>这一步主要是为了将训练数据进行拆分，一部分用来模型训练，另一部分用来测试训练的准确度</p>
<ol>
<li>导入模块</li>
</ol>
<p>前面，我们已经将数据全部处理好了，并且转换成了数值型</p>
<p><strong>我们使用sklearn中的数据选择模块</strong></p>
<pre class="highlight"><code class="">from sklearn.model_selection import train_test_split

type(train_test_split(attribute, tag, test_size=0.3))
</code></pre>
<blockquote>
<p>发现函数输出的是列表类型的数据</p>
</blockquote>
<ol start="2">
<li>拆分数据</li>
</ol>
<pre class="highlight"><code class="">a, b = [1,2]
</code></pre>
<p>我们是可以用这种方法赋值a和b的</p>
<ul>
<li>将数据分为70%用来训练，30%用来测试，数据的选取是随机的</li>
</ul>
<pre class="highlight"><code class="">x_train, x_test, y_train, y_test = train_test_split(attribute, tag, test_size=0.3)
</code></pre>
<ol start="3">
<li>改变数据标签</li>
</ol>
<p>由于上面数据的挑选是随机的，而且可以发现随机数据的标签，是混乱的，这可能会对我们后面的分析照成影响</p>
<ul>
<li>利用循环，来改变索引</li>
</ul>
<pre class="highlight"><code class="">for i in [x_train, x_test, y_train, y_test]:
    i.index = range(len(i.index))
</code></pre>
<h3 id="训练算法加预测"><a class="markdownIt-Anchor" href="#训练算法加预测"></a> 训练算法加预测</h3>
<ol>
<li>导入相关算法模块</li>
</ol>
<pre class="highlight"><code class="">from sklearn.tree import DecisionTreeClassifier
</code></pre>
<ol start="2">
<li>开始训练和预测</li>
</ol>
<ul>
<li>先实例化对象，再调用函数</li>
</ul>
<pre class="highlight"><code class="">clf = DecisionTreeClassifier(random_state=10)
clf.fit(x_train,y_train)
score = clf.score(x_test, y_test)

0.7574626865671642
</code></pre>
<h2 id="模型改进"><a class="markdownIt-Anchor" href="#模型改进"></a> 模型改进</h2>
<h3 id="交叉验证"><a class="markdownIt-Anchor" href="#交叉验证"></a> 交叉验证</h3>
<p><strong>定义：</strong><br />
交叉验证，就是重复的使用数据，把得到的样本数据进行切分，组合为不同的训练集和测试集。用训练集来训练模型，测试集来评估模型的好坏。在此基础上可以得到多组不同的训练集和测试集，某次训练集中的样本，在下次可能成为测试集中的样本</p>
<p><strong>交叉验证优点：</strong></p>
<blockquote>
<p>前面我们对数据集的划分，都是随机的，决策树在训练时，会表现出有时候训练的很好，有时候训练的不好，是因为对特征的选取和数据的选取都是随机的，有些数据在学习时，表现很好，在测试时，得分也不错，我们就应该将该训练集挑选出来，为了增强模型的泛化能力，因此可以使用交叉验证的方法</p>
</blockquote>
<ol>
<li>
<p>交叉验证用于评估模型的预测性能，尤其是训练好的模型在新数据上的表现，可以在一定程度上减小过拟合。</p>
</li>
<li>
<p>可以从有限的数据中获取尽可能多的有效信息。</p>
</li>
</ol>
<p><strong>重要参数说明：</strong></p>
<pre class="highlight"><code class="">cross_validation.cross_val_score(estimator, X, y=None, scoring=None, cv=None)

estimator要传入的算法
x表示完整的数据集
y表示标签
cv表示折叠次数
scoring衡量模型的指标，改变这个，则输出结果也会改变
</code></pre>
<pre class="highlight"><code class="">from sklearn.model_selection import cross_val_score

# 如果不输出mean()，会输出列表
clf = DecisionTreeClassifier(random_state=0)
score = cross_val_score(clf, attribute, tag, cv=10).mean()

score
array([0.71111111, 0.73333333, 0.71910112, 0.78651685, 0.80898876,
       0.76404494, 0.80898876, 0.79775281, 0.85393258, 0.80681818])

score.mean()
0.7790588468959255
</code></pre>
<h2 id="参数调节-重要"><a class="markdownIt-Anchor" href="#参数调节-重要"></a> 参数调节 (重要)</h2>
<h3 id="观察拟合程度"><a class="markdownIt-Anchor" href="#观察拟合程度"></a> 观察拟合程度</h3>
<p>在参数调节前，我们应当观察参数的拟合情况，观察是过拟合还是欠拟合</p>
<ul>
<li>max_depth 参数是决策树中常调节的参数，它能有效防止过拟合，限制决策树的最大深度(因为决策树每多加一层，就需要很多新的特征，因此深度过大，容易造成过拟合)</li>
</ul>
<p><strong>因此我们通过调节最大深度和画图，来观察过拟合情况</strong></p>
<pre class="highlight"><code class="">import matplotlib.pyplot as plt

# 设置两个列表来装载拟合分数
te = []
tr = []
for i in range(10):
    # 每一次循环下都要重新实例化模型
    clf = DecisionTreeClassifier(random_state=0, max_depth=i + 1)
    # 前面我们对数据进行了分类，30%用于测试，70%用于训练
    clf = clf.fit(x_train, y_train)
    # 对30%的测试数据进行打分
    train_score = clf.score(x_test, y_test)
    # 交叉验证法，要传入完整的数据集，训练也会不断改变类对象的属性
    test_score = cross_val_score(clf, attribute, tag, cv=10).mean()
    te.append(test_score)
    tr.append(train_score)
# 画图观察结果
plt.figure()    
plt.plot(range(1,11), tr, color='blue', label='test')
plt.plot(range(1,11), te, color='black',label='train')
# 改变x轴
plt.xticks(range(1,11))
# 增加图例
plt.legend()
plt.show()
</code></pre>
<p><img src="http://q6ip4it64.bkt.clouddn.com/3.23-1.png?e=1584956756&amp;token=IeqxMYJS9TcEnX8V6lUXD9FF_y3SCdOBApPAMpRy:VWQjje8QCILbKjD0onzu_vsZGxQ=&amp;attname=" alt="" /></p>
<ul>
<li>当我们改变参数时，发现当层数为6的时候，两者拟合的都比较好</li>
</ul>
<pre class="highlight"><code class="">    clf = DecisionTreeClassifier(random_state=0
                                 , max_depth=i + 1
                                , criterion='entropy'
                                 )
</code></pre>
<p><img src="http://q6ip4it64.bkt.clouddn.com/3.23-2.png?e=1584956756&amp;token=IeqxMYJS9TcEnX8V6lUXD9FF_y3SCdOBApPAMpRy:aI3mEh-v-QkEiU2s5ApMj6Ffbdk=&amp;attname=" alt="" /></p>
<h3 id="网格搜索"><a class="markdownIt-Anchor" href="#网格搜索"></a> 网格搜索</h3>
<pre class="highlight"><code class="">from sklearn.model_selection import GridSearchCV
clf = DecisionTreeClassifier(random_state=0)
parameters = {
    'criterion':['gini','entropy']
    ,'splitter':['best','random']
    ,&quot;max_depth&quot;:range(3,9)
}

# 网格搜索本身就会进行交叉验证
gs = GridSearchCV(clf, parameters, cv=10)

# 网格搜索就相当于新的算法，可以进行训练
gs = gs.fit(x_train, y_train)

print(gs.best_params_)# 查看最佳参数
print(gs.best_score_)# 查看跑的最好分数

{'criterion': 'entropy', 'max_depth': 3, 'splitter': 'best'}
0.8234349919743178
</code></pre>
<h2 id="结束"><a class="markdownIt-Anchor" href="#结束"></a> 结束</h2>
<h3 id="分析结果保存"><a class="markdownIt-Anchor" href="#分析结果保存"></a> 分析结果保存</h3>
<ul>
<li>当我们设置好算法时，要对数据进行保存，注意对象要是DataFrame</li>
</ul>
<pre class="highlight"><code class="">    file = pd.DataFrame({'PassengerId':test_data['PassengerId'], 'Survived':result})
    file.to_csv(r'C:\Users\asus\Desktop\test_result.csv')
</code></pre>

      
       
    </div>
</article>



<div class="article_copyright">
    <p><span class="copy-title">文章标题:</span>决策树实现Titanic(重要)</p>
    <p><span class="copy-title">文章字数:</span><span class="post-count">2.2k</span></p>
    <p><span class="copy-title">本文作者:</span><a  title="Miki Zhu">Miki Zhu</a></p>
    <p><span class="copy-title">发布时间:</span>2020-03-23, 17:09:05</p>
    <p><span class="copy-title">最后更新:</span>2020-03-23, 17:13:59</p>
    <span class="copy-title">原始链接:</span><a class="post-url" href="/2020/03/23/%E5%86%B3%E7%AD%96%E6%A0%91%E5%AE%9E%E7%8E%B0Titanic-%E9%87%8D%E8%A6%81/" title="决策树实现Titanic(重要)">http://mikiblog.online/2020/03/23/%E5%86%B3%E7%AD%96%E6%A0%91%E5%AE%9E%E7%8E%B0Titanic-%E9%87%8D%E8%A6%81/</a>
    <p>
        <span class="copy-title">版权声明:</span><i class="fa fa-creative-commons"></i> <a rel="license noopener" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
    </p>
</div>



    <div id="vcomment" class="comment"></div> 
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='//unpkg.com/valine/dist/Valine.min.js'></script>
<script src="https://cdnjs.loli.net/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
    var notify = 'false' == true ? true : false;
    var verify = 'false' == true ? true : false;
    new Valine({
        av: AV,
        el: '#vcomment',
        notify: notify,
        app_id: "3uM9nFQKJdCUOHqwT3LHL5Mt-gzGzoHsz",
        app_key: "3v4CdFJJjh97olrmyHxtdDlt",
        placeholder: "请输入",
        avatar:"mm",
    });
</script>



    





  <section id="comments" class="vcomment">
  </section>

    </div>
    <div class="copyright">
        <p class="footer-entry">©2016-2019 MikiZhu 赣ICP备20004640号</p>
<p class="footer-entry">Built with <a href="https://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/yelog/hexo-theme-3-hexo" target="_blank">3-hexo</a> theme</p>

    </div>
    <div class="full-toc">
        <button class="full"><span class="min "></span></button>
<button class="post-toc-menu"><span class="post-toc-menu-icons"></span></button>
<div class="post-toc"><span class="post-toc-title">目录</span>
    <div class="post-toc-content">

    </div>
</div>
<a class="" id="rocket" ></a>

    </div>
</div>
<div class="acParent"></div>

</body>
<script src="/js/jquery.pjax.js?v=1.0.1" ></script>

<script src="/js/script.js?v=1.0.1" ></script>
<script>
    var img_resize = 'default';
    /*作者、标签的自动补全*/
    $(function () {
        $('.search').AutoComplete({
            'data': ['#机器学习','#算法','#博客搭建','#python','#网站推荐','#考研经验',],
            'itemHeight': 20,
            'width': 418
        }).AutoComplete('show');
    })
    function initArticle() {
        /*渲染对应的表格样式*/
        
            $(".post .pjax table").addClass("green_title");
        

        /*渲染打赏样式*/
        

        /*高亮代码块行号*/
        

        /*访问数量*/
        
        $.getScript("//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js");
        

        /*代码高亮，行号对齐*/
        $('.pre-numbering').css('line-height',$('.has-numbering').css('line-height'));

        
        
    }

    /*打赏页面隐藏与展示*/
    

</script>

<!--加入行号的高亮代码块样式-->

<!--自定义样式设置-->
<style>
    
    
    .nav {
        width: 542px;
    }
    .nav.fullscreen {
        margin-left: -542px;
    }
    .nav-left {
        width: 120px;
    }
    
    
    @media screen and (max-width: 1468px) {
        .nav {
            width: 492px;
        }
        .nav.fullscreen {
            margin-left: -492px;
        }
        .nav-left {
            width: 100px;
        }
    }
    
    
    @media screen and (max-width: 1024px) {
        .nav {
            width: 492px;
            margin-left: -492px
        }
        .nav.fullscreen {
            margin-left: 0;
        }
        .nav .hide-list.fullscreen {
            left: 492px
        }
    }
    
    @media screen and (max-width: 426px) {
        .nav {
            width: 100%;
        }
        .nav-left {
            width: 100%;
        }
    }
    
    
    .nav-right .title-list nav a .post-title, .nav-right .title-list #local-search-result a .post-title {
        color: #383636;
    }
    
    
    .nav-right .title-list nav a .post-date, .nav-right .title-list #local-search-result a .post-date {
        color: #5e5e5f;
    }
    
    
    .nav-right nav a.hover, #local-search-result a.hover{
        background-color: #e2e0e0;
    }
    
    

    /*列表样式*/
    
    .post .pjax article .article-entry>ol, .post .pjax article .article-entry>ul, .post .pjax article>ol, .post .pjax article>ul{
        border: #e2dede solid 1px;
        border-radius: 10px;
        padding: 10px 32px 10px 56px;
    }
    .post .pjax article .article-entry li>ol, .post .pjax article .article-entry li>ul,.post .pjax article li>ol, .post .pjax article li>ul{
        padding-top: 5px;
        padding-bottom: 5px;
    }
    .post .pjax article .article-entry>ol>li, .post .pjax article .article-entry>ul>li,.post .pjax article>ol>li, .post .pjax article>ul>li{
        margin-bottom: auto;
        margin-left: auto;
    }
    .post .pjax article .article-entry li>ol>li, .post .pjax article .article-entry li>ul>li,.post .pjax article li>ol>li, .post .pjax article li>ul>li{
        margin-bottom: auto;
        margin-left: auto;
    }
    

    /* 背景图样式 */
    
    


    /*引用块样式*/
    

    /*文章列表背景图*/
    
    .nav-right:before {
        content: ' ';
        display: block;
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        opacity: 0.4;
        background: url("http://q8idlsxje.bkt.clouddn.com/%E6%B5%B7%E6%BB%A9.jpg");
        background-repeat: no-repeat;
        background-position: 50% 0;
        -ms-background-size: cover;
        -o-background-size: cover;
        -moz-background-size: cover;
        -webkit-background-size: cover;
        background-size: cover;
    }
    

    
</style>







  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  <script>
    var GUEST_INFO = ['nick','mail','link'];
    var guest_info = 'nick,mail,link'.split(',').filter(function(item){
        return GUEST_INFO.indexOf(item) > -1
    });
    var notify = 'false' == true;
    var verify = 'false' == true;
    new Valine({
      el: '.vcomment',
      notify: notify,
      verify: verify,
      appId: "3uM9nFQKJdCUOHqwT3LHL5Mt-gzGzoHsz",
      appKey: "3v4CdFJJjh97olrmyHxtdDlt",
      placeholder: "请输入",
      pageSize: '10',
      avatar: 'mm',
      lang: 'zh-cn',
      visitor: 'true'
    });
  </script>

</html>
